/**
 * 
 */
package com.xijing.util;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

/**
 * @author 小笨瓜
 * 
 */
public class RendedTable extends JTable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public RendedTable(DefaultTableModel model) {
		super(model);
		paintRow();
	}

	public void paintRow() {
		TableColumnModel tcm = this.getColumnModel();
		for (int i = 0, n = tcm.getColumnCount(); i < n; i++) {
			TableColumn tc = tcm.getColumn(i);
			tc.setCellRenderer(new TableRowRender());
		}
	}

	class TableRowRender extends DefaultTableCellRenderer {
		private static final long serialVersionUID = 1L;

		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int column) {
			if (row % 2 == 0) {
				setBackground(new Color(206, 231, 255)); // 设置偶数行底色
			} else if (row % 2 == 1) {
				setBackground(new Color(222, 231, 222)); // 设置奇数行底色
			}
			return super.getTableCellRendererComponent(table, value,
					isSelected, hasFocus, row, column);
		}
	}
}
